package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.AlbumInsertParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListVO;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/album")
@Api(tags = "04.相册管理模块")
@Slf4j
@Validated
public class AlbumController {

    @Autowired
    AlbumService service;
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult insert(@Valid AlbumInsertParam albumInsertParam){
            service.insert(albumInsertParam);
            return JsonResult.ok();
    }

    @PostMapping("/{albumId}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "albumId", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除相册失败，请提交合法的ID值")
                                  @PathVariable Long albumId) throws Exception {
        service.deleteById(albumId);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@Valid AlbumUpdateInfoParam albumUpdateInfoParam){
        service.update(albumUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查看相册")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false,paramType = "query")
    })
    public JsonResult list(@Range(min=1,message = "请输入合法的页码") Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageData<AlbumListVO> list = service.list(pageNum);
        return JsonResult.ok(list);
    }

    @GetMapping("/getStandard")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "albumId", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult getStandard(@Range(min = 1,message = "根据ID查看相册详情失败，请提交合法的ID值")
                             @RequestParam Long albumId) throws Exception {
        return JsonResult.ok(service.getStandardById(albumId));
    }

}
